<?php
/**
 * @authors liuqinglin 
 * @email (2961181429@qq.com) 
 * @(https://my.oschina.net/shp32131)
 * @date  2016-12-02 20:00:39
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Config extends Admin_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $userinfo = $this->session->userinfo;
        if(!isset($userinfo))
        {
            redirect('admin/login'); 
        }       
        $this->load->model('config_model');
    }
    //显示网站配置列表
    public function index()
    {
        $data['data'] = $this->config_model->get_all();

        $this->load->view('templates/header');
        $this->load->view('templates/menu');
		$this->load->view('config_list',$data);
    }
    //加载添加配置的表单视图
    public function add()
    {
        $this->load->helper('form');
        $this->load->view('templates/header');
        $this->load->view('templates/menu');
        $this->load->view('config_add');
    }
    //添加一个配置项
    public function insert()
    {
        $data = $this->input->post(array('name','value','description'),true);
        if($this->config_model->insert($data))
        {
            $this->success('添加配置成功！','admin/config');
        }
        else
        {
            $this->failure('添加失败！！！','admin/config/add');
        }
    }
    //删除一个配置项
    public function delete()
    {
        $id = $this->input->get('id');
        if($this->config_model->delete_one('id',$id))
        {   
            $this->success('删除配置成功！','admin/config');
        }
        else
        {
            $this->success('删除配置失败！','admin/config');            
        }
    }
    //加载修改表单视图
    public function edit()
    {
        $id = $this->input->get('id');
        if(empty($id))
        {
            $this->failure('检测不到id','admin/config');
        }
        $data = $this->config_model->get_one('id',$id);
        if(empty($data))
        {
            $this->failure('接收不到数据！','admin/config');
        }

        $this->load->helper('form');
        $this->load->view('templates/header');
        $this->load->view('templates/menu');
        $this->load->view('config_edit',$data);
    }
    //修改配置表单
    public function update(){
        //获取要修改的数据
        $data = $this->input->post(array('id','name','value','description'),true);
        
        $field['value']= $this->input->post('field');

        if(!isset($data) OR !isset($field['value']))
        {
            $this->failure('修改配置失败:不能获取要修改的数据！','admin/config');
        }
        //$field 条件字段
        $field['name']= 'id';
        
        if($this->config_model->update_one($data,$field))
        {
            $this->success('修改配置成功！','admin/config');
        }
        else
        {
            $this->failure('修改配置失败:调用函数update_one()出错！','admin/config');  
        }
    }

}